<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户登录</title>   
    <link rel="stylesheet" href="component/pear/css/pear.css" />
    <link rel="stylesheet" href="admin/css/other/login.css" />
</head>
<body background="admin/images/background.svg" style="background-size: cover;">
    <form class="layui-form" action="javascript:void(0);">
        <div class="layui-form-item">
            <img class="logo" src="admin/images/logo.png" />
            <div class="title">用户登录</div>
            <div class="desc">
                关爱残疾人，沟通无障碍。
            </div>
        </div>
        <div class="layui-form-item">
            <input placeholder="请输入登录账户" name="userName" lay-verify="required" hover class="layui-input" />
        </div>
        <div class="layui-form-item">
            <input placeholder="请输入登录密码" type="password" name="password" lay-verify="required" hover class="layui-input" />
        </div>       
        <div class="layui-form-item">
            <input type="checkbox" name="" title="记住密码" lay-skin="primary" checked>
        </div>
        <div class="layui-form-item">
            <button type="button" class="pear-btn pear-btn-success login" lay-submit lay-filter="login">
                登 录
            </button>
        </div>
    </form>
    <!-- 资 源 引 入 -->
    <script src="component/layui/layui.js"></script>
    <script src="component/pear/pear.js"></script>
    <script>
        layui.use(['form', 'jquery', 'button', 'popup', 'context'], function () {
            var form = layui.form;
            var button = layui.button;
            var popup = layui.popup;
            var $ = layui.jquery;
            var context = layui.context;      

            form.verify({
                account: function (value, item) {
                    if (value.length < 4) return '请输入至少4位账户';
                },
                password: function (value) {
                    if (value.length < 4) return '请输入至少4位密码';
                }
            });

            form.on('submit(login)', function (data) {
                var btn = button.load({ elem: '.login' })
                $.ajax({
                    url: '/login',
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'post',
                    success: function (result) {
                        if (result.type =='success') {
                            btn.stop(function () {
                                popup.success("登录成功", function () {
                                    location.href = "main.html";
                                    // 将 token 存储 localStore
                                    context.put("token", "Bearer " + result.result.token)
                                });
                            })
                        } else {
                            btn.stop(function () {
                                popup.failure("登录失败");
                            })
                        }
                    }
                })
                return false;
            });
        })
    </script>
</body>
</html>
